The Touch Controller (attached to the Gameboard prefab game object) exposes all the touch features available to creators. This allows creators to get information on the objects touching the board.

The Gameboard prefab is always available in your game as long as the SDK has been integrated. In order to access it from our custom Mono Behavior we just need to find it by its tag.

    GameObject gameboardObject = GameObject.FindWithTag("Gameboard");

With access to our Gameboard object we can then move on to obtain a reference to the Touch Controller. This is the controller that handles all the functions related to touch.

We will first need to define a reference to the controller to make it available throughout our script:

     TouchController touchController;

With our listener defined, we can now proceed to listen for touch events, captured as shapes. In our case we'll have functions called OnShapeFound, OnShapeUpdated, OnShapeLost, and AllShapesLost to handle these events.

    touchController = gameboardObject.GetComponent<TouchController>();
    touchController.OnShapeFound += OnShapeFound;
    touchController.OnShapeUpdated += OnShapeUpdated;
    touchController.OnShapeLost += OnShapeLost;
    touchController.AllShapesLost += AllShapesLost;

Now that we are able to receive the touch events, all that remains is to handle them in the game.

    void OnShapeFound(GameboardShape shape)
    {
        //Handle shape found
    }

    void OnShapeUpdated(GameboardShape shape)
    {
        //Handle shape updated
    }

    void OnShapeLost(GameboardShape shape)
    {
        //Handle shape lost
    }

    void AllShapesLost()
    {
        //Handle all shapes lost (no touch on the board)
    }

GameboardShape Properties

ShapeTypes

You can get specific shapes based on their id, or get all of the currently active shapes on the board.

In this example, imagine 123 is the current sessionId for a specific shape.

    GameboardShape shape = touchController.GetShape(123);

You can also get all of the current shapes touching the board:

    List<GameboardShape> shapes = touchController.GetShapes();

Based on v3.0.0+

You can also get the cotour information for the shapes that are on the board. This can be used to draw an outline around the shapes of the board, or to potentially create detailed collision meshes.

NOTE: In v3.0.0, the skipContourLogic flag is true by default on the Gameboard prefab object. That means the extra logic to process the contours will not be done. If you want to use contours, make sure that flag is set to false.

Step-by-Step

Mesh from contour

You can use the GameboardCreatorAssist to access the static method BuildMeshFromTrackedObject, this takes a GameboardShape and the location the shape in in to return a mesh object built with the contours. You can also provide the camera and distance from camera so the mesh is in the correct location based on your camera.

Mesh mesh = GameboardCreatorAssist.BuildMeshFromTrackedObject(shape, content.transform, new Mesh(), distanceFromCamera, gameCamera);

Line from contour

Or, you can access the contour values directly on the GameboardShape object to use as you would like. In this example, we are adding lines showing where the contours are.

Step-by-Step

The UpdateContour method would be called in the OnShapeUpdated listener method.

private void UpdateContour(GameboardShape shape)
{
    GameObject lineRendererObject = new GameObject("DrawContourLine", typeof(LineRenderer));

    var lineRenderer = lineRendererObject.GetComponent<LineRenderer>();
    lineRenderer.startWidth = 0.1f;
    lineRenderer.endWidth = 0.1f;
    lineRenderer.loop = true;

    var contourWorldVectors3D = shape.GetContourWorldVectors(Camera.main.transform.position.y);

    lineRenderer.positionCount = contourWorldVectors3D.Length;
    for (int i = 0; i < contourWorldVectors3D.Length; i++)
    {
        lineRenderer.SetPosition(i, contourWorldVectors3D[i]);
    }
}

Although we are working in a managed environment it is always a good idea to clean the listeners when no longer needed.

    void OnDestroy()
    {
        touchController.OnShapeFound -= OnShapeFound;
        touchController.OnShapeUpdated -= OnShapeUpdated;
        touchController.OnShapeLost -= OnShapeLost;
        touchController.AllShapesLost -= AllShapesLost;
    }

This section include the entire code in one single, easy to copy section.

    TouchController touchController;

    // Start is called before the first frame update
    void Start()
    {
        GameObject gameboardObject = GameObject.FindWithTag("Gameboard");
        touchController = gameboardObject.GetComponent<TouchController>();
        touchController.OnShapeFound += OnShapeFound;
        touchController.OnShapeUpdated += OnShapeUpdated;
        touchController.OnShapeLost += OnShapeLost;
        touchController.AllShapesLost += AllShapesLost;
    }

    void OnShapeFound(GameboardShape shape)
    {
        //Handle shape found
    }

    void OnShapeUpdated(GameboardShape shape)
    {
        //Handle shape updated
    }

    void OnShapeLost(GameboardShape shape)
    {
        //Handle shape lost
    }

    void AllShapesLost()
    {
        //Handle all shapes lost (no touch on the board)
    }

    void OnDestroy()
    {
        touchController.OnShapeFound -= OnShapeFound;
        touchController.OnShapeUpdated -= OnShapeUpdated;
        touchController.OnShapeLost -= OnShapeLost;
        touchController.AllShapesLost -= AllShapesLost;
    }